package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.UserVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.SysLogServiceImp;
import com.school.movie.service.imp.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    SysLogServiceImp logService;
    @Value("${upload.absPath}")
    String absPath;

    @RequestMapping("/list")
    public String list(PageBean pager, UserVo user, HttpSession session, ModelMap map) {
        if (user == null || user.getMobile() == null) {
            if (session.getAttribute("user") != null) {
                user = (UserVo) session.getAttribute("user");
            }
        } else {
            session.setAttribute("user", user);
        }
        int cnt = userService.count(user);
        pager.setTotalRows(cnt);
        List<UserVo> userList = userService.list(user, pager);
        pager.setData(userList);
        map.addAttribute("pager", pager);
        return "user";
    }

    @RequestMapping("/del")
    public String del(String ids, PageBean pager, ModelMap map) {
        String[] arr = ids.split(",");
        userService.del(arr);
        logService.add(BaseConstants.LOG_DEL, "删除了编号为：" + ids + "的考区");
        UserVo u = new UserVo();
        int cnt = userService.count(u);
        pager.setTotalRows(cnt);
        List<UserVo> userList = userService.list(u, pager);
        pager.setData(userList);
        map.addAttribute("pager", pager);
        return "user";
    }

    @RequestMapping("/update")
    public String update(String mobile, ModelMap map) {
        UserVo user = userService.getById(mobile);
        map.addAttribute("user", user);
        return "userAdd";
    }

    @RequestMapping("/init")
    public String init(ModelMap map) {
        return "userAdd";
    }

    /**
     * 保存用户信息
     *
     * @param user
     * @param map
     * @param file1
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(UserVo user, ModelMap map, MultipartFile file1) throws Exception {
        UserVo u = userService.getById(user.getMobile());
        if (u != null) {
            if (file1.getOriginalFilename().equals("")) {
                user.setHeadImg(u.getHeadImg());
            } else {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file1.getOriginalFilename();
                System.out.println("--上传文件名-->>" + oriName);
                String extName = oriName.substring(oriName.lastIndexOf("."));

                String newFileName = picName + extName;
                File targetFile = new File(absPath, newFileName);
                // 保存文件
                file1.transferTo(targetFile);
                user.setHeadImg("card/" + newFileName);
            }
            userService.update(user);
            logService.add(BaseConstants.LOG_UPDATE, user.toString());
        } else {
            if (file1.getOriginalFilename().equals("")) {
                user.setHeadImg("card/headImg.png");
            } else {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file1.getOriginalFilename();
                System.out.println("--上传文件名-->>" + oriName);
                String extName = oriName.substring(oriName.lastIndexOf("."));

                String newFileName = picName + extName;
                File targetFile = new File(absPath, newFileName);
                // 保存文件
                file1.transferTo(targetFile);
                user.setHeadImg("card/" + newFileName);
            }
            userService.add(user);
            logService.add(BaseConstants.LOG_ADD, user.toString());
        }
        u = new UserVo();
        int cnt = userService.count(u);
        PageBean pager = new PageBean();
        pager.setTotalRows(cnt);
        List<UserVo> userList = userService.list(u, pager);
        pager.setData(userList);
        map.addAttribute("pager", pager);
        return "user";
    }
}
